package com.dtsz.m2.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.CountPowerVo;
import com.dtsz.m2.utils.TextCountdownUtils;
import com.dtsz.m2.volley.state.HUCState;
/**
 * 算力加成信息
 */
public class ForgetActivity extends BaseActivity {

    TextView tvTime, tvYes, tvNo, tvCountUser, tvActiveUser, tvFormula;
    TextCountdownUtils myCountdownUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("总算力");
    }

    @Override
    public void initView() {
        super.initView();
        tvTime = findViewById(R.id.tv_time);
        tvYes = findViewById(R.id.tv_yes);
        tvNo = findViewById(R.id.tv_no);
        tvCountUser = findViewById(R.id.tv_count_user);
        tvActiveUser = findViewById(R.id.tv_active_user);
        tvFormula = findViewById(R.id.tv_formula);
    }


    @Override
    public void initClick() {
        super.initClick();
        getStartMining();
    }


    private void getStartMining() {
        hucRequestInterfase.executeStr(this, CountPowerVo.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.COUNT_POWER_URL,
                hucParameInterfase.getRequestParameter(pageVo), true);
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (response instanceof CountPowerVo) {
                    CountPowerVo countPowerVo = (CountPowerVo) response;
                    tvTime.setText(countPowerVo.getEndTime());
                    tvYes.setText(countPowerVo.getMiningYes());
                    tvNo.setText(countPowerVo.getMiningNo());
                    tvCountUser.setText(countPowerVo.getCountUser());
                    tvActiveUser.setText(countPowerVo.getActiveUser());
                    tvFormula.setText(countPowerVo.getFormula());
                    myCountdownUtils = new TextCountdownUtils(this, tvTime, Long.parseLong(countPowerVo.getFormulaLong()), 1000);
                    myCountdownUtils.start();

                } else {
                    String code = response.toString();
                    if ("结束挖矿".equals(code)) {
                        Intent data = new Intent();
                        setResult(Activity.RESULT_OK, data);
                        finish();
                    } else {
                        showToast(response.toString());
                    }
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + inputType);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myCountdownUtils != null) {
            myCountdownUtils.cancel();
        }
    }
}